package com.ukao.cashregister.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.usbrfidreader.UsbrfidreaderHelper;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.ThreadUtils;
import com.ukao.cashregister.widget.ClearEditText;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 绑标
 */
public class BindSignDialogFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.bing_code_edit)
    ClearEditText bingCodeEdit;
    @BindView(R.id.edit_name_cancel)
    Button editNameCancel;
    @BindView(R.id.edit_name_submit)
    Button editNameSubmit;
    Unbinder unbinder;
    @BindView(R.id.rfid_btn)
    StateImageView rfidBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;



    public static BindSignDialogFragment newInstance(String param1, String param2) {
        BindSignDialogFragment fragment = new BindSignDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bing_sign_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        title.setText("绑标");
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
        UsbrfidreaderHelper.getInstance().readCardNum();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        UsbrfidreaderHelper.getInstance().cancelReadCardNum();
        unbinder.unbind();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected void initListener() {
        super.initListener();
        bingCodeEdit.requestFocus();  //获取焦点
        bingCodeEdit.setOnEditorActionListener(mOnEditorActionListener);
    }

    private TextView.OnEditorActionListener mOnEditorActionListener = (v, actionId, event) -> {
        L.i("event=" + event + "actionId=" + actionId);
        if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
            String strbingCode = getText(bingCodeEdit);
            if (!CheckUtils.isEmpty(strbingCode)) {
                strbingCode(strbingCode);
            }
        }

        return false;
    };

    @Subscribe
    public void onEvent(MembershipCardEvent event) {

        switch (event.getMessage()) {
            case add:/*成功*/
                String rfidSecretCard = (String) event.getData();
                strbingCode(rfidSecretCard);
                break;






        }
    }

    @OnClick({R.id.close_btn, R.id.edit_name_cancel, R.id.edit_name_submit,R.id.rfid_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();

            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.edit_name_submit:
                String strbingCode = getText(bingCodeEdit);
                if (CheckUtils.isEmpty(strbingCode)) {
                    return;
                }
                strbingCode(strbingCode);

                break;
            case  R.id.rfid_btn :
                UsbrfidreaderHelper.getInstance().readCardNum();

                break;
        }
    }

    public void strbingCode(String strbingCode) {
        ThreadUtils.runOnUiThread(() -> {
            if (mOnDialogSureClickListener != null) {
                mOnDialogSureClickListener.onSureItemClick(strbingCode);
            }
            dismiss();
        });

    }



}
